package com.nbot;


import com.nbot.server.BotServer;
import com.nbot.server.domain.TaskDO;
import com.nbot.server.properties.NbotProperties;
import com.nbot.server.properties.TaskThreadPoolProperties;
import com.nbot.server.service.JobService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.jms.Queue;
import java.io.IOException;
import java.util.List;


/**
 * Created by xianxuehui on 2018/4/11
 */
@EnableSwagger2
@EnableConfigurationProperties({TaskThreadPoolProperties.class, NbotProperties.class})
@SpringBootApplication
//@EnableScheduling
@EnableJms
@EnableCaching
@EnableTransactionManagement
public class StartApplication implements ApplicationRunner {
    public static void main(String[] args) throws IOException {
        SpringApplication.run(StartApplication.class, args);

    }

    @Bean
    public Queue queue() {
        return new ActiveMQQueue("mytest.queue");
    }

    @Autowired
    BotServer botServer;

    @Autowired
    NbotProperties nbotProperties;
    @Autowired
    JobService jobService;

    @Override
    public void run(ApplicationArguments args) throws Exception {


        if (!nbotProperties.isUnitTestMode()) {
            //启动计划任务里所有设置为"开启"的任务
            List<TaskDO> jobs = jobService.list(null);
            for (TaskDO taskDO : jobs) {
                if ("1".equals(taskDO.getJobStatus())) {
                    jobService.changeStatus(taskDO.getId(), "start");
                }
            }
            botServer.init();





        }

    }
}
